import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

public class DirectorySizeCalculator {

    public static void main(String[] args) {
        if (args.length > 0) {
            String directoryPath = args[0];
            File directory = new File(directoryPath);
            if (directory.isDirectory()) {
                try {
                    long size = calculateDirectorySize(directory.toPath());
                    System.out.println("Directory size: " + size + " bytes");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println(directoryPath + " is not a directory.");
            }
        } else {
            System.out.println("Usage: java DirectorySizeCalculator <directory_path>");
        }
    }

    private static long calculateDirectorySize(Path path) throws IOException {
        final long[] size = {0};

        Files.walkFileTree(path, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE,
                new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                        try {
                            size[0] += Files.size(file);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult visitFileFailed(Path file, IOException exc) {
                        // Print the error and continue with other files.
                        System.err.println("Error accessing file: " + file + " " + exc);
                        return FileVisitResult.CONTINUE;
                    }
                });

        return size[0];
    }
}
